package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.SupplierListConverter;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private SupplierListConverter supplierListConverter;

    //查询所有物资来源

    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum,
                                               Integer pageSize,
                                               String name,
                                               String address,
                                               String contact,
                                               SupplierVO supplierVO) {

        PageHelper.startPage(pageNum,pageSize);

        Example example = new Example(Supplier.class);
        Example.Criteria criteria = example.createCriteria();
        List<Supplier> suppliers = null;
        if (name!=null&&!"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if (address!=null&&!"".equals(address)){
            criteria.andLike("address","%"+address+"%");
        }
        if (contact!=null&&!"".equals(contact)){
            criteria.andLike("contact","%"+contact+"%");
        }



        suppliers=supplierMapper.selectByExample(example);


        List<SupplierVO> supplierVOS = supplierListConverter.supplierVOList(suppliers);



        PageInfo<Supplier> info=new PageInfo<>(suppliers);

        return new PageVO<>(info.getTotal(),supplierVOS);
    }

   //添加物质来源

    @Override
    public Boolean add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        int insert = supplierMapper.insert(supplier);
        if (insert>0){
            return true;
        }
        return false;
    }

    //删除物资来源
    @Override
    public Boolean delete(Integer id) {
        int i = supplierMapper.deleteByPrimaryKey(id);
        if (i>0){
            return true;
        }
        return false;
    }

    /*编辑前回显数据*/

    @Override
    public SupplierVO edit(Integer id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        return supplierVO;
    }

    @Override
    public Boolean update(SupplierVO supplierVO, Integer id) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setModifiedTime(new Date());
        int isUpdate = supplierMapper.updateByPrimaryKey(supplier);
        if (isUpdate>0){
            return true;
        }
        return false;
    }
}
